/*
 * Copyright (c) 2017, Li Bo/Alex Lee All rights reserved.
 * 
 * http://www.libo4dream.club/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.hyts.bmsp.tool;

/**
 * @ClassName: PageUtil
 * @Description: 分页操作工具类服务实现功能
 * @author: LiBo/Alex Lee
 * @date: 2017年9月21日 下午10:44:21
 * @email: alexlibochn@china-pui.com.cn/alex.link@foxmail.com
 */
public class PageUtil {

	/**
	 * @fieldName: pageNo
	 * @fieldType: String
	 * @Description: 页号
	 */
	private Integer pageNo = 1;
	
	/**
	 * @fieldName: pageSize
	 * @fieldType: String
	 * @Description: 页大小
	 */
	private Integer pageSize = 10;
	
	/**
	 * @fieldName: pageNum
	 * @fieldType: String
	 * @Description: 页数目
	 */
	private Integer pageNum = 0;
	
	
	private Integer pageCount = 0;
	
	
	public Integer getPageNo() {
		return pageNo;
	}

	
	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	
	/**
	 * @Title:PageUtil
	 * @Description:TODO
	 * @param pageNo
	 * @param pageCount
	 */
	public PageUtil(Integer pageNo, Integer pageCount) {
		super();
		this.pageNo = pageNo;
		this.pageCount = pageCount;
	}


	public static class MysqlPageModel{
		
		public Integer getStart() {
			return start;
		}

		public Integer getPageSize() {
			return pageSize;
		}

		private Integer start;
		
		private Integer pageSize;
		
		private String pageSqlSubfix;

		public String getPageSqlSubfix() {
			return pageSqlSubfix;
		}
		
	}
	
	public Integer getPageCount() {
		return pageCount;
	}


	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}


	/**
	 * @Title: getMysqlModel
	 * @Description: 获取mysql分页模型
	 * @return
	 * @return: MysqlPageModel
	 */
	public  MysqlPageModel getMysqlModel(){
		
		MysqlPageModel model = new MysqlPageModel();
		
		model.pageSize = this.pageSize;
		
		this.pageNum = this.pageCount%this.pageSize > 0? (this.pageCount/this.pageSize)+1:this.pageCount/this.pageSize;
		
		//防止页面异常
		if(this.pageNo < 1 )
		{
			this.pageNo = 1;
		}
		if(this.pageNo > this.getPageNum())
		{
			this.pageNo = this.pageNum;
		}
		model.start = model.pageSize * (pageNo - 1);
		model.pageSqlSubfix = "limit"+model.start+","+model.getPageSize();
		return model;
	}
	
	
	public static void main(String[] args) {
		PageUtil page = new PageUtil(3,40);
		MysqlPageModel model = page.getMysqlModel();
		System.out.println(model.pageSqlSubfix);
	}
	
	
}
